import java.util.Scanner;

public class HomeWorkTest1 {
    public static void Q(String args[])
    {
        A a = new A();
        B b = new B(a);
        b.start();
        a.start();
    }
}

class A extends Thread
{
    private boolean loop = true;

    @Override
    public void run() {
        while (loop)
        {
            System.out.println((int)(Math.random()*100+1));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public void setLoop(boolean loop)
    {
        this.loop = loop;
    }
}

class B extends Thread
{
    private A a;
    public B(A a)
    {
        this.a = a;
    }

    @Override
    public void run() {
        while (true)
        {
            System.out.println("请输入一个指令(Q)退出");
            Scanner scanner = new Scanner(System.in);
            char ch = scanner.next().toUpperCase().charAt(0);
            if(ch == 'Q')
            {
                a.setLoop(false);
                System.out.println("B线程已退出");
                break;
            }
        }
    }
}